{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pymysql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['672981058', '290372201', '887997105', '545384211', '972982368', '672988487', '205430684', '672756810', '587929620', '247977835', '205472783', '887997105', '417943498', '672844804', '375432715', '247997928', '972995357', '587969375', '802934945', '887834112', '417960044', '375487315', '887819067', '460498130', '247815161', '802970900', '802880914', '332905144', '930431171', '247937692', '375452051', '332922333', '417983744', '845449064', '760425621', '205411207', '802963270', '502971234', '630375962', '247937692', '545476263', '845332679', '247954746', '757994895', '205396001', '845371318', '545454307', '247881749', '332936171', '672915241', '630390320', '332936171', '375461117', '332997676', '502958341', '417880800', '290460767', '290418422', '630487616', '375461117', '205456219', '930293735', '715260245', '930310137', '545336222', '887907538', '672952856', '290454682', '930457229', '930457229', '930415802', '460402676', '845341389', '332945268', '247971582', '545388887', '760452879', '760381076', '715381126', '930415802', '375270376', '332964901', '715412935', '332953210', '845470552', '332981699', '630482641', '205408299', '417902684', '460473914', '930465645', '290400725', '460406858', '887949982', '887928999', '715337603', '760380647', '930464655', '672880811', '375427803']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def getOidData():\n",
    "    try:\n",
    "        conn = pymysql.connect(host=\"localhost\",\n",
    "                           port = 3306,\n",
    "                           user = \"root\",\n",
    "                           password = \"1234\",\n",
    "                           db = \"bc\")\n",
    "        cursor = conn.cursor()\n",
    "        sql = \"\"\"\n",
    "            select oid from hot_content;\n",
    "        \"\"\"\n",
    "        cursor.execute(sql)\n",
    "        oids = cursor.fetchall()\n",
    "        return [oid[0] for oid in oids]\n",
    "    except Exception as e:\n",
    "        print(\"出错了\", e)\n",
    "    finally:\n",
    "        if conn:\n",
    "            conn.close()\n",
    "        if cursor:\n",
    "            cursor.close()\n",
    "\n",
    "print(getOidData())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "def getConnAndCursor():\n",
    "     try:\n",
    "        conn = pymysql.connect(host=\"localhost\",\n",
    "                           port = 3306,\n",
    "                           user = \"root\",\n",
    "                           password = \"1234\",\n",
    "                           db = \"bc\")\n",
    "        cursor = conn.cursor()\n",
    "        return (conn ,cursor)\n",
    "     except Exception as e:\n",
    "         print(\"链接出错了\", e)\n",
    "         return None\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "def closeConnAndCursor(conn, cursor):\n",
    "    conn.close\n",
    "    cursor.close\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['672981058', '290372201', '887997105', '545384211', '972982368', '672988487', '205430684', '672756810', '587929620', '247977835', '205472783', '887997105', '417943498', '672844804', '375432715', '247997928', '972995357', '587969375', '802934945', '887834112', '417960044', '375487315', '887819067', '460498130', '247815161', '802970900', '802880914', '332905144', '930431171', '247937692', '375452051', '332922333', '417983744', '845449064', '760425621', '205411207', '802963270', '502971234', '630375962', '247937692', '545476263', '845332679', '247954746', '757994895', '205396001', '845371318', '545454307', '247881749', '332936171', '672915241', '630390320', '332936171', '375461117', '332997676', '502958341', '417880800', '290460767', '290418422', '630487616', '375461117', '205456219', '930293735', '715260245', '930310137', '545336222', '887907538', '672952856', '290454682', '930457229', '930457229', '930415802', '460402676', '845341389', '332945268', '247971582', '545388887', '760452879', '760381076', '715381126', '930415802', '375270376', '332964901', '715412935', '332953210', '845470552', '332981699', '630482641', '205408299', '417902684', '460473914', '930465645', '290400725', '460406858', '887949982', '887928999', '715337603', '760380647', '930464655', '672880811', '375427803']\n"
     ]
    }
   ],
   "source": [
    "def getOidData():\n",
    "    try:\n",
    "        conn = pymysql.connect(host=\"localhost\",\n",
    "                           port = 3306,\n",
    "                           user = \"root\",\n",
    "                           password = \"1234\",\n",
    "                           db = \"bc\")\n",
    "        cursor = conn.cursor()\n",
    "        sql = \"\"\"\n",
    "            select oid from hot_content;\n",
    "        \"\"\"\n",
    "        cursor.execute(sql)\n",
    "        oids = cursor.fetchall()\n",
    "        return [oid[0] for oid in oids]\n",
    "    except Exception as e:\n",
    "        print(\"出错了\", e)\n",
    "    finally:\n",
    "        if conn:\n",
    "            conn.close()\n",
    "        if cursor:\n",
    "            cursor.close()\n",
    "\n",
    "print(getOidData())\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________我是法è\\x80' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________我是法è\\x80' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________我要当å½' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲___________//i/————/法老⎮Ii/_________<带兄弟们再冲' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''╱/i╱╲_______________//i/————/我要抓?⎮Ii/_________<我要当å' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '要保持年轻，永远热泪盈眶'', 0.0283876413580439)' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '有些人'而非'你'明显会更严谨，观众也会更舒服，但可悲的æ\\x98' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.______╱/i╱╲___________//i/————/⎮Ii/_________<老师牛逼！/\\\\' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.______╱/i╱╲______________//i/————/就是他拉坏了⎮Ii/______' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.______╱/i╱╲______________//i/————/就是他拉坏了⎮Ii/______' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.______╱/i╱╲______________//i/————/就是他拉坏了⎮Ii/______' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.______╱/i╱╲______________//i/————/就是他拉坏了⎮Ii/______' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '行'改成'刑'才更有威慑力[OK]', 2.2749538453e-06)' at line 1\")\n",
      "(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '能用水洗掉的算脏吗？'，其实现在最脏的是人心罢了。看看é' at line 1\")\n",
      "1\n",
      "执行完成！\n"
     ]
    }
   ],
   "source": [
    "def getDataFromOid(oid):\n",
    "    conn, cursor = getConnAndCursor()\n",
    "    sql = \"\"\"\n",
    "        SELECT * FROM `bc`.`{}`\n",
    "    \"\"\".format(oid)\n",
    "    cursor.execute(sql)\n",
    "    data = cursor.fetchall()\n",
    "    closeConnAndCursor(conn, cursor)\n",
    "    return list(data)\n",
    "\n",
    "\n",
    "def insertValue(oid):\n",
    "    ods = getDataFromOid(oid)\n",
    "    conn, cursor = getConnAndCursor()\n",
    "    for od in ods:\n",
    "        try:\n",
    "            sql = \"\"\"\n",
    "                INSERT INTO `bc`.`relies_end`(`rpid`, `oid`, `uname`, `sex`, `like`, `current_level`, `lmessage`, `emotion_value`) VALUES ('{}', '{}', '{}', '{}', {}, {}, '{}', {})\n",
    "            \"\"\".format(od[0], oid, od[1], od[2], od[3], od[4], od[5], od[6])\n",
    "            cursor.execute(sql)\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "    conn.commit()\n",
    "    closeConnAndCursor(conn, cursor)\n",
    "\n",
    "\n",
    "def concatTable():\n",
    "    count = 0\n",
    "    oids =  getOidData()\n",
    "    for oid in oids:\n",
    "        insertValue(oid)\n",
    "    count += 1\n",
    "    print(count)\n",
    "\n",
    "# sql = insertValue(672981058)\n",
    "# sql\n",
    "concatTable()\n",
    "print(\"执行完成！\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}